﻿using LUtilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EquipSecs300
{
    public class E84_Timer
    {
        private double _stopTime = 0;
        SystemTimeCounter _oriTimer = new SystemTimeCounter();

        /// <summary>
        /// 超时时间设置(0.1s)
        /// </summary>
        public E84_Timer(double setTime)
        {
            SetTime = setTime;
        }
       
        private double _setTime = 0;
        /// <summary>
        /// 超时时间设置(0.1s)
        /// </summary>
        public double SetTime
        {
            get { return _setTime / 100; }
            set { _setTime = value * 100; }
        }

        double _uLimit = 99999999;
        /// <summary>
        /// 最大经过时间
        /// </summary>
        public double ULimit
        {
            get => _uLimit;
            set => _uLimit = value > 0 ? value : 99999999;
        }

        private bool _isTiming = false;
        /// <summary>
        /// T:正在计时，F:停止计时
        /// </summary>
        public bool IsTiming
        {
            get => _isTiming;
        }

        private bool _isTimeOut = false;
        /// <summary>
        /// 是否超时
        /// </summary>
        public bool IsTimeOut
        {
            get
            {
                return (_oriTimer.Duration >= _setTime) && _isTiming;
            }
        }

        private double _duration;
        /// <summary>
        /// 经过的时间值
        /// </summary>
        public double Duration
        {
            get => (_isTiming ? (_oriTimer.Duration > _uLimit ? _uLimit : _oriTimer.Duration) : _stopTime);
        }

        /// <summary>
        /// 开始计时(仅第一次有效，重复开始不会重新计时)
        /// </summary>
        public void Start()
        {
            if (!_isTiming)
            {
                _isTiming = true;
                _oriTimer.Start();
            }
        }

        /// <summary>
        /// 停止计时()
        /// </summary>
        public void Stop()
        {
            if (_isTiming)
            {
                _isTiming = false;
                _stopTime = _oriTimer.Duration;
                _oriTimer.Stop();
            }
        }
    }
}
